﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Drawing;
using System.Xml.Linq;

namespace CarInfoBusiness
{
    public class GetInfos
    {

        private static GetInfos gi;

        public static GetInfos Instance
        {
            get
            {
                if (gi == null)
                    gi = new GetInfos();
                return gi;
            }
        }

        private GetInfos()
        { }

        //public List<CarInfoDBAccess.Model.CarInfo> GetCars(CarInfoDBAccess.Model.CarType ct)
        //{

        //}

        public List<CarInfoDBAccess.DBAccess_New.CSCarInfo> GetCar(CarInfoDBAccess.DBAccess_New.CSCarType ct)
        {
            XElement xCar = Common.Instance.GetXml(ct.Url);
            if (xCar == null)
                return null;
            List<CarInfoDBAccess.DBAccess_New.CSCarInfo> ciList = new List<CarInfoDBAccess.DBAccess_New.CSCarInfo>();
            var data = from node in xCar.Descendants()
                       select node;
            foreach (var node in data)
            {
                if (node.Name.LocalName == "div")
                {
                    if (node.HasAttributes && node.Attributes("class").Count() == 1)
                        if (node.Attribute("class").Value == "left_7s")
                        {
                            foreach (XElement item in node.Descendants())
                            {
                                if (item.Name.LocalName == "li")
                                {
                                    if (item.Attributes("class").Count() == 1)
                                        if (item.Attribute("class").Value == "c")
                                        {
                                            string DomainName = "http://db.chinacars.com";
                                            CarInfoDBAccess.DBAccess_New.CSCarInfo ci = new CarInfoDBAccess.DBAccess_New.CSCarInfo();
                                            // ci.Id = ++LastCarInfoId;
                                            //ci.CtID = ct.Id;
                                            XElement xeInfo = item.Descendants().Single(a => a.Value == "[参数]");
                                            XElement xePic = item.Descendants().Single(a => a.Value == "[酷图]");
                                            ci.InfoUrl = DomainName + xeInfo.Attribute("href").Value;
                                            ci.PicUrl = DomainName + xePic.Attribute("href").Value;
                                            ciList.Add(ci);
                                        }
                                }


                            }  //   foreach (XElement item in node.Descendants())           
                        }//if (node.Attribute("class").Value == "left_7s")
                }//if (node.Name.LocalName == "div")
            }
            foreach (var item in ciList)
            {
                GetCarInfo(item.InfoUrl, item);
                GetCarPic(item.PicUrl, item);
                Common.Instance.TextOutput(item.Name);

            }
            return ciList;
        }

        private void GetCarInfo(string url, CarInfoDBAccess.DBAccess_New.CSCarInfo ci)
        {
            XElement xeInfo = Common.Instance.GetXml(url);

            string id = url.Replace("http://db.chinacars.com/1/db_", "");
            id = id.Replace(".htm", "");
            ci.Id = Convert.ToInt32(id);

            if (xeInfo == null)
                return;
            var data = from n in xeInfo.Descendants()
                       select n;
            int flag = 0;
            foreach (var item in data)
            {
                if (item.Name.LocalName == "td" && item.Value == "车型名称")
                {
                    flag = 1;
                    continue;
                }
                if (item.Name.LocalName == "td" && item.Value == "车型厢式")
                {
                    flag = 2;
                    continue;
                }
                if (item.Name.LocalName == "td" && item.Value == "厂商指导价(万元)")
                {
                    flag = 3;
                    continue;
                }
                if(item.Name.LocalName=="li")
                    if (item.HasAttributes && item.Attributes("class").Count() == 1)
                        if (item.Attribute("class").Value == "wenzi")
                        {
                            ci.Typename = item.Value;
                            continue;
                        }
                switch (flag)
                {
                    case 1:
                        ci.Name = item.Value;
                        flag = 0;
                        break;
                    case 2:
                        ci.Type = item.Value;
                        flag = 0;
                        break;
                    case 3:
                        ci.Price = Convert.ToDouble(item.Value);
                        flag = 0;
                        break;
                    default:
                        break;
                }
            }
        }

        private void GetCarPic(string url, CarInfoDBAccess.DBAccess_New.CSCarInfo ci)
        {
            //XElement xePic = GetXml(url);
            //if (xePic == null)
            //    return;
            string html = Common.Instance.GetHtml(url);
            string[] lines = html.Split('\n');
            int flag = 0;
            foreach (string item in lines)
            {
                if (item.StartsWith("var imgUrl=["))
                {
                    flag = 1;
                    continue;
                }
                if (flag == 1)
                {
                    string PicUrl = item.Replace("\"", "");
                    PicUrl = PicUrl.Replace("\r", "");
                    PicUrl = PicUrl.Replace(",", "");
                   // ci.Pic = Common.Instance.GetPic(PicUrl);
                   // ci.Img = Common.Instance.ImageToByteArray(ci.Pic);
                    ci.Img = Common.Instance.GetPic(PicUrl);
                    
                   // ci.Image = Common.Instance.ImageToByteArray(Common.Instance.GetPic(PicUrl));
                    flag = 0;
                    return;
                }
            }
        }

    }
}
